<?php
if (!defined('IN_IA')) {
	exit('Access Denied');
}
if (!function_exists('getIsSecureConnection')) {
    function getIsSecureConnection()
    {
//        return isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1)
//            || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0;
        if (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) {
            return true;
        } elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'])) {
            return true;
        }
        return false;
    }
}
if (function_exists('getIsSecureConnection'))
{
    $secure = getIsSecureConnection();
    $http = $secure ? 'https' : 'http';
    $_W['siteroot'] = strexists($_W['siteroot'],'https://') ? $_W['siteroot'] : str_replace('http',$http,$_W['siteroot']);
}
require_once IA_ROOT . '/addons/mx_shop/version.php';
require_once IA_ROOT . '/addons/mx_shop/defines.php';
require_once MX_SHOP_INC . 'functions.php';
class Mx_shopModuleSite extends WeModuleSite {

	public function getMenus(){
		global $_W;
		return array(
				array(
					'title' => '管理后台',
					'icon'=>'fa fa-shopping-cart',
					'url'=> webUrl()
				)
		);
	}
	public function doWebWeb() {

	    //管理用户id
	    $this->checkAdmin();

        //保存平台id
        $this->inPlatform();

	    //后台路由
		m('route')->run();
	}
	public function doMobileMobile() {
		m('route')->run(false);
	}
	public function payResult($params) {
		return m('order')->payResult($params);
	}

	protected function checkAdmin()
    {
        !defined('ADMIN_ID') && define('ADMIN_ID', 1);
    }

    /*
     * 检查平台id
     * @param int $do 0=web 1=mobile
     * return bool
     * */
    protected function inPlatform($do=0){

	    global $_W, $_GPC;

	    if(empty($_W['uniacid'])){
            message('公众号信息错误');
        }

        $platform = pdo_fetch("SELECT id,platformid,name FROM " . tablename('mx_shop_platform') . " WHERE uniacid=:uniacid AND deleted=0 LIMIT 1", array(':uniacid' => $_W['uniacid']));

        //没有关联平台
        if(empty($platform))
        {
            //如果是mobile端显示错误信息
            if($do == 1){
                show_json(0, '平台信息错误');
            }

            //强制跳转到平台
            if($_W['uid']==ADMIN_ID ){
                $r = explode('.', $_GPC['r']);
                if(!in_array('platform', $r))
                {
                    message('请添加该公众号对应的平台', webUrl('platform'), 'error');exit();
                }
            }else{
                message('平台信息错误');
            }
            return false;
        }

        return $_W['platformid'] = $platform['platformid'];
    }
}
